package cn.quang.search.toolkit.component.items.impl;

import cn.quang.search.toolkit.component.AbstractSearchItem;
import cn.quang.search.toolkit.component.constants.Constants;
import cn.quang.search.toolkit.enums.DataSourceTypeEnum;
import cn.quang.search.toolkit.provider.entity.KeyOperatorValue;
import cn.quang.search.toolkit.provider.entity.QuerySlice;

import java.util.Arrays;
import java.util.Collections;

/**
 * between a and b
 * @author: 杨联泉
 * @since: 2018/04/16 15:15
 */
public class BetweenSearchItem extends AbstractSearchItem {

    private final Object fromValue;
    private final Object toValue;

    public BetweenSearchItem(DataSourceTypeEnum dsType, String fieldDic, Object fromValue, Object toValue){
        super(dsType, fieldDic, null);
        this.fromValue = fromValue;
        this.toValue = toValue;
    }

    @Override
    public QuerySlice getQuerySlice() {
        String sqlTemplate = String.format("%s between %s and %s", Constants.FIELD, Constants.VALUE, Constants.VALUE);
        return QuerySlice.of(
                sqlTemplate,
                KeyOperatorValue.of(col, Collections.singletonList(this.col), Collections.emptyList(), Arrays.asList(fromValue, toValue))
        );
    }
}
